package com.ahzy.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ahzy.po.User;
import com.ahzy.service.UserService;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@RestController
@RequestMapping("/user")
@Slf4j
@Api
public class UserContriller {

	@Autowired
	UserService userService;

	@ApiOperation(value = "管理员登录")
	@GetMapping("/login")
	public Map<String, Object> login(@RequestParam(value = "number", required = true) String number,
			@RequestParam(value = "password", required = true) String password) {
		Map<String, Object> mapData = new HashMap<>();
		User user = userService.loginUser(number, password);
		
		if (!(user == null)) {
			if(user.getType()==1) {
				mapData.put("state", false);
				mapData.put("msg", "抱歉，您的账号正在审核中");
				return mapData;
			}else if(user.getType()==0){
				mapData.put("state", false);
				mapData.put("msg", "抱歉，您未通过审核，请联系社长或副社长");
				return mapData;
			}else {
				mapData.put("state", true);
				mapData.put("msg", "登陆成功");
				mapData.put("user", user);
				return mapData;
			}
		} else {
			mapData.put("state", false);
			mapData.put("msg", "登陆失败");
			return mapData;
		}
	}

	@ApiOperation(value = "管理员注册")
	@GetMapping("/regist")
	public Map<String, Object> regist(@RequestParam(value = "number", required = true) Integer number,
			@RequestParam(value = "password", required = true) String password,
			@RequestParam(value = "name", required = true) String name,
			@RequestParam(value = "department", required = true) String department,
			@RequestParam(value = "major", required = true) String major,
			@RequestParam(value = "phone", required = true) String phone,
			@RequestParam(value = "sex", required = true) String sex,
			@RequestParam(value = "type", required = true) Integer type,
			@RequestParam(value = "age", required = true) Integer age,
			@RequestParam(value = "remarks", required = true) String remarks) {
		Map<String, Object> mapData = new HashMap<>();
		User user = new User();
		user.setNumber(number);
		user.setPassword(password);
		user.setName(name);
		user.setDepartment(department);
		user.setMajor(major);
		user.setPhone(phone);
		user.setSex(sex);
		user.setAge(age);
		user.setRemarks(remarks);
		user.setType(type);
		user.setDateline(new Date());
		user.setJointime(new Date());
		
		User userNumber = userService.selectOneByNumber(user.getNumber());
		if(userNumber==null) {
			// 新增操作
			int i=userService.insertWithoutPrimaryKey(user);
			if (i==1) {
				mapData.put("state", true);
				mapData.put("msg", "注册成功，等待管理员审核通过");
				mapData.put("user", user);
			} else {
				mapData.put("state", false);
				mapData.put("msg", "注册失败");
				
			}
		}else {
			mapData.put("state", false);
			mapData.put("msg", "该学号已注册");
		}
		return mapData;
		
		
	}

	@ApiOperation(value = "获取列表分页数据")
	@GetMapping("/data")
	public Map<String, Object> data(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
			@RequestParam(value = "limit", required = false, defaultValue = "10") int limit,
			@RequestParam(value = "name", required = false) String name,
			@RequestParam(value = "type", required = false) String type,
			@RequestParam(value = "gteType", required = false) String gteType) {
		Map<String, Object> mapData = new HashMap<>();
		Example userExample = new Example(User.class);
		Criteria userCriteria = userExample.createCriteria();
		//添加模糊查询
		if(name!=null) {
			userCriteria.andLike("name", "%" + name+ "%");
		}
		if(type!=null) {
			userCriteria.andEqualTo("type",type);
		}
		//type 大于等于
		if(gteType!=null) {
			userCriteria.andGreaterThanOrEqualTo("type", gteType);
		}
		PageInfo<User> rolePageInfo = userService.selectByExamplePage(page, limit, userExample);
		mapData.put("code", 0);
		mapData.put("state", false);
		mapData.put("total", rolePageInfo.getTotal());
		mapData.put("data", rolePageInfo.getList());
		return mapData;
	}

	
	@ApiOperation(value = "有主键则修改，否则新增")
	@PostMapping("/save")
	public Map<String, Object> save(@Validated User user) {
		System.out.println(user.toString());
		Map<String, Object> mapData = new HashMap<>();
		try {
			if (user.getId() == 0||user.getId() == null) {
				
				User userNumber = userService.selectOneByNumber(user.getNumber());
				System.out.println(userNumber);
				System.out.println("123");
				if(userNumber==null) {
					// 新增操作
					user.setDateline(new Date());
					userService.insertWithoutPrimaryKey(user);
					mapData.put("state", true);
				}else {
					mapData.put("state", false);
					mapData.put("msg", "该学号已注册");
				}
				
				
			} else {
				// 修改操作
				// 当审批通过时，更新加入时间
				if(user.getType()==2) {
					user.setJointime(new Date());
				}
				userService.updateByPrimaryKeySelective(user);
				mapData.put("state", true);
			}
		} catch (Exception e) {
			mapData.put("state", false);
			log.error("{}", e.getMessage());
		}
		return mapData;
	}

	@ApiOperation(value = "通过id删除管理员信息")
	@DeleteMapping("/remove/{id}")
	public Map<String, Object> delete(@PathVariable Integer id) {
		Map<String, Object> mapData = new HashMap<>();
		try {
			userService.deleteByPrimaryKey(id);
			mapData.put("state", true);
		} catch (Exception e) {
			mapData.put("state", false);
			log.error("delete()方法发生异常：{}", e.getMessage());
		}
		return mapData;
	}

	@ApiOperation(value = "通过id查看管理员信息")
	@GetMapping("/detail/{id}")
	public Map<String, Object> detail(@PathVariable Integer id) {
		Map<String, Object> mapData = new HashMap<>();
		User User = userService.selectByPrimaryKey(id);
		mapData.put("data", User);
		mapData.put("state", false);
		return mapData;
	}

}
